package com.bdd.controller;

import com.bdd.Result.Result;
import com.bdd.model.bo.CategoryBO;
import com.bdd.model.bo.CategoryMonthSalesAmountBO;
import com.bdd.model.bo.CategorySalesAmountBO;
import com.bdd.model.bo.CategorySalesVolumeBO;
import com.bdd.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author huanghaining
 * @date 2022/3/18
 */
@RestController
@RequestMapping("/category")
@Api(value = "类别管理")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "分类列表")
    @RequiresRoles(value={"STAFF","ADMIN"},logical= Logical.OR)
    public Result<List<CategoryBO>> list(){
        List<CategoryBO> categoryBOList = categoryService.list();
        return Result.ok(categoryBOList);
    }

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "增加分类")
    @RequiresRoles({"ADMIN"})
    public Result add(CategoryBO bo){
        categoryService.add(bo);
        return Result.ok();
    }

    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "编辑分类")
    @RequiresRoles({"ADMIN"})
    public Result edit(CategoryBO bo){
        if(Objects.isNull(bo.getId())){
            throw new IllegalArgumentException("类别id不可为空");
        }
        categoryService.edit(bo);
        return Result.ok();
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除分类")
    @RequiresRoles({"ADMIN"})
    public Result delete(Integer id){
        if(Objects.isNull(id)){
            throw new IllegalArgumentException("类别id不可为空");
        }
        categoryService.delete(id);
        return Result.ok();
    }

    @GetMapping("/searchByName")
    @ResponseBody
    @ApiOperation(value = "根据分类名称搜索分类")
    @RequiresRoles({"ADMIN"})
    public Result<List<CategoryBO>> searchByName(String name){
        if(StringUtils.isBlank(name)){
            throw new IllegalArgumentException("商品名称不可为空");
        }
        List<CategoryBO> categoryBOList = categoryService.searchByName(name);
        return Result.ok(categoryBOList);
    }

    @GetMapping("/getSalesAmountGroupByCate")
    @ResponseBody
    @ApiOperation(value = "获取每个类别的销售金额")
    @RequiresRoles({"ADMIN"})
    public Result<List<CategorySalesAmountBO>> getSalesAmountGroupByCate(){
        List<CategorySalesAmountBO> categorySalesAmountBOS = categoryService.getSalesAmountGroupByCate();
        return Result.ok(categorySalesAmountBOS);
    }

    @GetMapping("/getSalesVolumeGroupByCate")
    @ResponseBody
    @ApiOperation(value = "获取每个类别的销售量")
    public Result<List<CategorySalesVolumeBO>> getSalesVolumeGroupByCate(){
        List<CategorySalesVolumeBO> categorySalesVolumeBO = categoryService.getSalesVolumeGroupByCate();
        return Result.ok(categorySalesVolumeBO);
    }

    @GetMapping("/getSalesAmountByMountGroupByCate")
    @ResponseBody
    @ApiOperation(value = "获取某个类别某个月的销售金额")
    @RequiresRoles({"ADMIN"})
    public Result<List<CategoryMonthSalesAmountBO>> getSalesAmountByMonthtGroupByCate(String year,String month){
        if(StringUtils.isBlank(year)||StringUtils.isBlank(month)){
            throw new IllegalArgumentException("年份或月份不可问空");
        }
        List<CategoryMonthSalesAmountBO> categoryMonthSalesAmountBOList = null;
        try{
            categoryMonthSalesAmountBOList = categoryService.getSalesAmountByMonthtGroupByCate(year,month);
        }catch (Exception e){
            System.out.println("捕捉异常");
        }
        return Result.ok(categoryMonthSalesAmountBOList);
    }


}
